package com.localjoycommunity.cpdj.project.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.localjoycommunity.cpdj.project.system.domain.User;
import com.localjoycommunity.cpdj.project.system.domain.UserActivity;
import com.localjoycommunity.cpdj.project.system.domain.vo.UserPartyActivityHistoryVo;
import com.localjoycommunity.cpdj.project.system.domain.vo.UserPartyActivityListVo;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Optional;

/**
 * 用户活动Mapper接口
 *
 * @author leepandar
 * @date 2020-11-16
 */
public interface UserActivityMapper extends BaseMapper<UserActivity> {
    /**
     * 查询用户活动
     *
     * @param id 用户活动ID
     * @return 用户活动
     */
    public UserActivity selectUserActivityById(Long id);

    /**
     * 查询用户活动列表
     *
     * @param userActivity 用户活动
     * @return 用户活动集合
     */
    public List<UserActivity> selectUserActivityList(UserActivity userActivity);

    /**
     * 新增用户活动
     *
     * @param userActivity 用户活动
     * @return 结果
     */
    public int insertUserActivity(UserActivity userActivity);

    /**
     * 修改用户活动
     *
     * @param userActivity 用户活动
     * @return 结果
     */
    public int updateUserActivity(UserActivity userActivity);
    public int updateUserActivityByActivityId(UserActivity userActivity);

    /**
     * 删除用户活动
     *
     * @param id 用户活动ID
     * @return 结果
     */
    public int deleteUserActivityById(Long id);

    /**
     * 批量删除用户活动
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteUserActivityByIds(Long[] ids);

    List<UserActivity> selectActivityHistory(User user);

    @Select("select state_code from user_activity where user_id=#{userId} and activity_id =#{activityId};")
    Optional<Integer> isApplyPartyActivity(Long userId, Long activityId);

    @Select("SELECT aw.id,aw.title,aw.apply_start_time,aw.apply_end_time,aw.start_time,aw.end_time,aw.cover,ua.state_code " +
            "FROM activity_wish as aw left join user_activity as ua on aw.id=ua.activity_id " +
            "where user_id=#{userId} " +
            "and state_code=#{stateCode};")
    List<UserPartyActivityListVo> userPartyActivityList(Long userId, int stateCode);

    @Select("SELECT ua.id,ua.state_code,ua.update_time,aw.title,u.name FROM user_activity as ua left join `user` as u on ua.user_id=u.id left join activity_wish as aw on ua.activity_id=aw.id;")
    List<UserPartyActivityHistoryVo> userPartyActivityHistoryList();
}
